package processing.main.clock;

import processing.core.*; 

import android.util.Log;
import android.view.MotionEvent; 
import android.view.KeyEvent; 
import android.graphics.Bitmap; 
import java.io.*; 
import java.util.*; 


public class Clock extends PApplet {
    private int maxTextWidth;
    private final int maxTextHeight = 40;
    private final int minTextHeight = 20;
	PFont f;
public void setup() {
	background(255);
	orientation(PORTRAIT);
	maxTextWidth = floor(screenWidth - screenWidth/5);
	f = createFont("HelveticaLTStd-LightCond.otf", 30);

	
}

public void draw(){
	fill(255);
	rect(0,0, sketchWidth(), sketchHeight());
	ArrayList<Event> events = DummyDataGenerator.getEvents();
	for(int i = 0 ; i < events.size(); i++){
		drawRect(events.get(i));
	}
	drawHourInfo();
}

public void drawRect(Event currentEvent) {
	fill(currentEvent.getColorRed(), currentEvent.getColorGreen(), currentEvent.getColorBlue(), 255);
	rect(0, (float)(1.0 - currentEvent.getTopPosition())*sketchHeight(), screenWidth, (currentEvent.getRectangleHeight()) * sketchHeight());
	
	fill(0);
	drawEventInfo(currentEvent);
	
}

public void drawEventInfo(Event currentEvent){
	String eventName = currentEvent.getEventName();
	String eventLocation = currentEvent.getEventLocation();

	float eventHeight = currentEvent.getRectangleHeight() * sketchHeight();
	float eventTop = (float)(1.0 - currentEvent.getTopPosition())*sketchHeight();

    float textHeight;
	if(eventHeight > minTextHeight + 9){
		// Can show both
		textHeight = (eventHeight - 9)/2;
		if(textHeight > maxTextHeight)
			textHeight = maxTextHeight;
		textFont(f,textHeight);
		eventName = truncateText(eventName);
		eventLocation = truncateText(eventLocation);
		text(eventName, screenWidth-textWidth(eventName), eventTop + textHeight + 3);
		text(eventLocation, screenWidth-textWidth(eventLocation), eventTop + (textHeight*2) + 6);
	}
	else if(eventHeight > minTextHeight + 6){
		textHeight = (eventHeight - 6)/2;
		textFont(f,textHeight);
		eventName = truncateText(eventName);
		text(eventName, textWidth(eventName), eventTop + textHeight + 3);	
	}
	
}

public String truncateText(String text){
	while(text.length() != 0 && textWidth(text) > maxTextWidth){
		text = text.substring(0, text.length()-1);
	}
	return text;
}

public void drawHourInfo(){
	textFont(f);
	fill(0);
	stroke(0);
	strokeWeight(5);
	text("Now", 5, 30);
	line(0,30, screenWidth/5, 30);
	text("1 Hour", 5, (float)(sketchHeight()*.25 - 5));
	line((float)0.0, (float)(sketchHeight()*.25), (float)screenWidth/5, (float)(sketchHeight()*.25));
	text("3 Hours", 5, (float)(sketchHeight()*.50 - 5));
	line((float)0.0, (float)(sketchHeight()*.50), (float)screenWidth/5, (float)(sketchHeight()*.50));
	text("6 Hours", 5, (float)(sketchHeight()*.75 - 5));
	line((float)0.0, (float)(sketchHeight()*.75), (float)screenWidth/5, (float)(sketchHeight()*.75));
	text("12 Hours", 5, (float)(sketchHeight()*1.0 - 5));
	line((float)0.0, (float)(sketchHeight()*1.0), (float)screenWidth/5, (float)(sketchHeight()*1.0));
	noStroke();
}


void randomColorRed(){
    fill(random(195,254), random(0,254),random(0, 50), random(155, 255));
}

  public int sketchWidth() { return screenWidth; }
  public int sketchHeight() { return screenHeight - 30; }
}

